<?php
/**
 * Object Relational Persistence Mapping Class for table: sale_program
 *
 * @package	com.athena
 * @author	LHV
 */

$loader->requireOnce('ordo/SaleProgramStore.class.php');

class SaleProgram extends ORDataObject {

	/**#@+
	 * Fields of table: sale_program mapped to class members
	 */
	var $sale_program_id		= '';
	var $vi_name		= '';
	var $en_name		= '';
	var $vi_description		= '';
	var $en_description		= '';
	var $code		= '';
	/**#@-*/
	
	/**
	 * DB Table
	 */
	var $_table = 'sale_program';

	/**
	 * Primary Key
	 */
	var $_key = 'sale_program_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'SaleProgram';

	/**
	 * Handle instantiation
	 */
	function SaleProgram() {
		parent::ORDataObject();
	}

	function checkCode($code, $saleId) {
		// new record
		if (empty($saleId)) {
			return $this->checkDuplicatedCode($code);
		}
		
		// edit record
		$sql = "SELECT code from $this->_table where sale_program_id = $saleId";
		$res = $this->_execute($sql);
		$oldCode = $res->fields['code'];
		
		// code has no change
		if ($oldCode == $code) {
			return 1;
		}
		
		// change code
		return $this->checkDuplicatedCode($code);
	}
	
	function checkDuplicatedCode($code) {
		$sql = "SELECT * from $this->_table where code = '$code'";
		$res = $this->_execute($sql);
		// duplicated code
		if ($res && !$res->EOF) {
			return 0;
		}
		return 1;
	}
	
	function getCurrentSaleProgramsForOption() {
		$ds = new Datasource_sql();
		$today = date('Y-m-d');
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$sql = array(
			'cols'	=> "store.sale_program_store_id , CONCAT_WS(' - ', s.code, s.$field) code",
			'from'	=> "$this->_table s
				INNER JOIN sale_program_store store ON s.sale_program_id = store.sale_program_id",
			'where'	=> "end_date >= DATE('$today')",
			'orderby' => "s.code DESC"
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('sale_program_store_id', 'code');
	}
	
	/**
	 * Get all sale programs with current_date <= end_date.
	 *
	 * @return unknown
	 */
	function getCurrentSalePrograms() {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$ds = new Datasource_sql();
		$today = date('Y-m-d');
		$disformat = L10nHelper::getSqlDateFormat();
		$sql = array(
			'cols'	=> "store.sale_program_store_id ,s.$field name , s.code,
				DATE_FORMAT(store.start_date, '$disformat') start_date,
				DATE_FORMAT(store.end_date, '$disformat') end_date",
			'from'	=> "$this->_table s
				INNER JOIN sale_program_store store ON s.sale_program_id = store.sale_program_id",
			'where'	=> "end_date >= DATE('$today')",
			'orderby' => "name"
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	function getNotCurrentSalePrograms() {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$ds = new Datasource_sql();
		$today = date('Y-m-d');
		$sql = "SELECT s.sale_program_id , s.$field name
				FROM sale_program s
				WHERE s.sale_program_id NOT IN (SELECT store.sale_program_id
												FROM sale_program_store store
												WHERE store.end_date >= DATE('$today') );
				";
		$res = $this->_execute($sql);
		return $res;
	}
	
	/**
	 * Get all sale programs.
	 *
	 * @return List of sale programs
	 */
	function getAllSalePrograms() {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$ds = new Datasource_sql();
        $sql = array(
            'cols'  => "sale_program_id, $field name",
            'from'  => $this->_table,
            'orderby' => "name"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray('sale_program_id', 'name');
	}
	/**
	 * Get all sale programs.
	 *
	 * @return List of sale programs
	 */
	public function listAll() {
		$nameField	=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$desField 	=	L10nHelper::changeField(L10nHelper::getLocale(),"description");
		$ds = new Datasource_sql();
		$sql = " SELECT sale_program_id sale_program_id, $nameField as name, $desField as description, code
                 FROM $this->_table
                 ORDER BY name;";
		$res = $this->_execute($sql);
		return $res;
	}
	
	function isExist() {
		$sql = "SELECT $this->_key from $this->_table
			where $this->_key = " . $this->get_id();
		$res = $this->_execute($sql);
		if ($res && !$res->EOF) {
			return true;
		}
		return false;
	}
}

?>